package com.catazy.mvvmlib.ext

import java.security.MessageDigest

/**
 * 作者: Tao
 * 日期: 2024/1/29
 * 时间: 11:07
 * 描述: 加密拓展类
 */

/**
 * MD5加密
 */
fun String.md5(): String {
    if (this.isEmpty()) return ""
    val md5: MessageDigest
    try {
        md5 = MessageDigest.getInstance("MD5")
        val bytes = md5.digest(this.toByteArray())
        val result = StringBuilder()
        for (b in bytes) {
            var temp = Integer.toHexString(b.toInt() and 0xff)
            if (temp.length == 1) {
                temp = "0$temp"
            }
            result.append(temp)
        }
        return result.toString()
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return ""
}